$file1 = "RETRIEVE-ALTERNATE-OUTPUT-FORMAT-ACTION.txt";
$file2 = "probeset_gene_position.txt";

open(BUFF1,$file1);
open(BUFF2,$file2);
open(WRBUFF,">3_most_output.txt");
$line = <BUFF1>;
while($line = <BUFF1>)
{
	chomp $line;
	$line =~ s/\n//g;
	$line =~ s/\r//g;
	@array = split("\t",$line);
	$gene_direc{$array[0]} = $array[1];
	$gene_chr{$array[0]} = $array[4]; 
}
$line = <BUFF2>;
while($line = <BUFF2>)
{
        chomp $line;
        $line =~ s/\n//g;
        $line =~ s/\r//g;
        @array = split("\t",$line);
	
	if ( $gene_probeset{$array[1]} eq "k")
	{
		${$array[1]}[$num{$array[1]}] = $array[0];
                $num{$array[1]}++;
	}
	else
	{
		$gene_probeset{$array[1]} = "k";
		$num{$array[1]} = 0;
		${$array[1]}[$num{$array[1]}] = $array[0];
		$num{$array[1]}++;	
	}
	@array_pos = split('///',$array[2]);
	$num1{$array[0]} = 0;
	for($i=0;$i<@array_pos;$i++)
	{
		@{'array_sep_chrloc'.$i} = split('//',$array_pos[$i]);
		@{'array_sep_chrloc_ali'.$i} = split(' ',${'array_sep_chrloc'.$i}[0]);
		@{'array_chr'.$i} = split(":",${'array_sep_chrloc_ali'.$i}[0]);
		${'array_chr'.$i}[0] =~ s/chr//g;
		@{'array_cord'.$i} = split('-',${'array_chr'.$i}[1]);
		${'array_sep_chrloc_ali'.$i}[1] =~ s/\(//g;
		${'array_sep_chrloc_ali'.$i}[1] =~ s/\)//g;
		#print $array_chr[0]."\t".$array_cord[0]."\t".$array_cord[1]."\t".$array_sep_chrloc_ali[1]."\n";
		if(${'array_chr'.$i}[0] eq $gene_chr{$array[1]})
		{
			${"probeset_coord".$array[0]}[$num1{$array[0]}] =${'array_cord'.$i}[0];
			${"probeset_coord_conf".$array[0]}[$num1{$array[0]}] =${'array_sep_chrloc'.$i}[1];
			$num1{$array[0]}++;
			${"probeset_coord".$array[0]}[$num1{$array[0]}] =${'array_cord'.$i}[1];
			${"probeset_coord_conf".$array[0]}[$num1{$array[0]}] =${'array_sep_chrloc'.$i}[1];
			$num1{$array[0]}++; 
		}
	}	
	#$probeset_pos{$array[0]} = 
	#print scalar(@array_pos)."\n";
	#print "@{'probeset_coord'.$array[0]}\n";
}
#print @{'probeset_coord209459_s_at'};
#print "@{'probeset_coord214736_s_at'}\n@{'probeset_coord_conf214736_s_at'}\n";
while (($key, $value) = each(%gene_probeset))
{
	#print $key."\n";
	$m =0;
	
    if(scalar(@{$key}) == 1)
     {
	print WRBUFF $key."\t".${$key}[0]."\n";
     }
    else
     {
	for($i=0;$i<@{$key};$i++)
	{
		$pr_set = ${$key}[$i];
		for($j=0;$j<@{"probeset_coord".${$key}[$i]};$j++)
		{	
			#if($key==18){
			#print ${"probeset_coord".${$key}[$i]}[$j]."\n";}
			${'temparray'.$key}[$m] = ${"probeset_coord".${$key}[$i]}[$j];
			${'temparray2s'.$key}[$m] = ${"probeset_coord_conf".${$key}[$i]}[$j];
			${'temparray1s'.$key}[$m] = $pr_set;
			$m++;		
			
		}
	}
	if($key == 11163)
	{ #print "@temparray\n@temparray1\n"; 
		#print "@temparray11163\n@temparray1s11163\n@temparray2s11163\n";
	}
	if($gene_direc{$key} eq "-")
	{
		
                 @{'index'.$key} = sort{ ${'temparray'.$key}[ $a ] <=> ${'temparray'.$key}[ $b ] } 0 .. $#{'temparray'.$key};
                @{'temparray'.$key} = sort {$a <=> $b}@{'temparray'.$key};

                for($i=0;$i<@{'temparray'.$key};$i++)
                {
                        ${'temparray11s'.$key}[$i] = ${'temparray1s'.$key}[${'index'.$key}[$i]];
                        ${'temparray22s'.$key}[$i] = ${'temparray2s'.$key}[${'index'.$key}[$i]];


                }
                $m1 =0;
                for($i=0;$i<@{'temparray'.$key};$i++)
                {
                        if(${'temparray'.$key}[0] == ${'temparray'.$key}[$i])
                        {
                                ${'newarray'.$key}[$m1] = ${'temparray22s'.$key}[$i];
                                ${'newarray1s'.$key}[$m1] = ${'temparray11s'.$key}[$i];
                                $m1++;
                        }

                }
                @{'index1'.$key} = sort{ ${'newarray'.$key}[ $b ] <=> ${'newarray'.$key}[ $a ]} 0 .. $#{'newarray'.$key};
                @{'newarray'.$key} =  sort {$b <=> $a}@{'newarray'.$key};
                for($i=0;$i<@{'index1'.$key};$i++)
                {
                        ${'newarray11s'.$key}[$i] = ${'newarray1s'.$key}[${'index1'.$key}[$i]];
                }
		if(${'newarray'.$key}[0] == ${'newarray'.$key}[1])
                {
                        for($j=0;$j<@{'newarray'.$key};$j++)
                        {
                                if(${'newarray'.$key}[0] == ${'newarray'.$key}[$j])
                                {
                                        if(${'newarray11s'.$key}[$j] =~ m/\d+_at/)
                                        {
                                                $li = ${'newarray11s'.$key}[$j];
                                        }
                                }
                        }
                        if($li eq "")
                        {
                                print WRBUFF "$key\t${'newarray11s'.$key}[0]\n";
                        }
                        else
                        {
                                print WRBUFF "$key\t$li\n";
                        }
                }
                else
                {
                        print WRBUFF "$key\t${'newarray11s'.$key}[0]\n";
                }
	
	}
	elsif($gene_direc{$key} eq "+")
	{	
		 @{'index'.$key} = sort{ ${'temparray'.$key}[ $b ] <=> ${'temparray'.$key}[ $a ] } 0 .. $#{'temparray'.$key};
                @{'temparray'.$key} = sort {$b <=> $a}@{'temparray'.$key};

                for($i=0;$i<@{'temparray'.$key};$i++)
                {
                        ${'temparray11s'.$key}[$i] = ${'temparray1s'.$key}[${'index'.$key}[$i]];
                        ${'temparray22s'.$key}[$i] = ${'temparray2s'.$key}[${'index'.$key}[$i]];
        	         

                }
                $m1 =0;
		 if($key == 11163)
	        { #print "@temparray\n@temparray1\n";
        	        #print "@temparray11163\n@temparray11s11163\n@temparray22s11163\n";
        	}

                for($i=0;$i<@{'temparray'.$key};$i++)
                {
                        if(${'temparray'.$key}[0] == ${'temparray'.$key}[$i])
                        {
                                ${'newarray'.$key}[$m1] = ${'temparray22s'.$key}[$i];
                                ${'newarray1s'.$key}[$m1] = ${'temparray11s'.$key}[$i];
                                $m1++;
                        }

                }
		if($key == 11163)
                { #print "@temparray\n@temparray1\n";
                        #print "@newarray11163\n@newarray1s11163\n";
                }

                @{'index1'.$key} = sort{ ${'newarray'.$key}[ $b ] <=> ${'newarray'.$key}[ $a ]} 0 .. $#{'newarray'.$key};
                @{'newarray'.$key} =  sort {$b <=> $a}@{'newarray'.$key};
                for($i=0;$i<@{'index1'.$key};$i++)
                {
                        ${'newarray11s'.$key}[$i] = ${'newarray1s'.$key}[${'index1'.$key}[$i]];
                }
		if(${'newarray'.$key}[0] == ${'newarray'.$key}[1])
		{
			for($j=0;$j<@{'newarray'.$key};$j++)
			{
		 		if(${'newarray'.$key}[0] == ${'newarray'.$key}[$j])
                		{
					if(${'newarray11s'.$key}[$j] =~ m/\d+_at/)
					{
						$li = ${'newarray11s'.$key}[$j];
					}
                		}
			}
			if($li eq "")
			{	
				print WRBUFF "$key\t${'newarray11s'.$key}[0]\n";
			}
			else
			{
				print WRBUFF "$key\t$li\n";
			}
		}
		else
		{
                	print WRBUFF "$key\t${'newarray11s'.$key}[0]\n";
		}

	}
	else
	{
		 print WRBUFF "$key\tnull\n";
	}
   }
}

